package org.traffometer.traffobase.model;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.opendmtp.server.base.Packet;
import org.opendmtp.server.db.Payload;

/**
 * Tests PacketParser class.
 * @author George Lee
 *
 */
public class TestPacketParser {

  /**Test GPS data.*/
  private static final String lat0 = "100.22298";
  private static final String long0 = "62.467";
  private static final String time0 = "11:28:49";

  private static final String lat1 = "100.2799";
  private static final String long1 = "62.978";
  private static final String time1 = "11:42:37";

  /**Test payload for the packet.*/
  private static final String testString = "lat0=" + lat0 + " long0=" + long0 + 
  " time0=" + time0 + " lat1=" + lat1 + " long1=" + long1 + " time1=" + time1;
  
  private static final Payload testPayload = new Payload();

  /**Test packet.*/
  private Packet testPacket;
  
  /**Parses the test packet.*/
  private PacketParser testParser;
  
  /**
   * Test the creation of the packet.  Check that it has the correct values.
   */
  @Test
  public void testNormalOperation() {
    //Write string to payload and test that the entire string is written.
    int stringLength = this.testPayload.writeString(this.testString, this.testString.length());
    assertEquals("Testing the length of the string.", stringLength, this.testString.length());
    
    //Create packet.
    this.testPacket = Packet.createServerPacket(0, this.testPayload);
    
    //Parse the packet.
    this.testParser = new PacketParser(this.testPacket);
    
    //Check values in testParser.
    assertEquals("Retrieving the starting latitude.", lat0, this.testParser.getStartLatitude());
    assertEquals("Retrieving the starting longitude.", long0, this.testParser.getStartLongitude());
    assertEquals("Retrieving the starting time.", time0, this.testParser.getStartTimestamp());
    assertEquals("Retrieving the ending latitude.", lat1, this.testParser.getEndingLatitude());
    assertEquals("Retrieving the ending longitude.", long1, this.testParser.getEndingLongitude());
    assertEquals("Retrieving the ending time.", time1, this.testParser.getEndingTimestamp());
    
  }
  
}
